<?php

namespace app\api\model;

use think\Model;
use traits\model\SoftDelete;

class Circles extends Model
{
    use SoftDelete;

    protected $deleteTime = 'deletetime';
    protected $hidden = ['deletetime', 'updatetime'];

    public function getLogoAttr($value)
    {
        return $value ? request()->domain().$value : '';
    }
    public function getCreateTimeAttr($value)
    {
        return $value ? date('Y-m-d', $value) : '';
    }

    public static function getCircles($category_id, $length)
    {
        $where = [];
        if ($category_id) {
            $where['category_id'] = $category_id;
        }
        return self::with(['design'])->withCount('like')->where($where)->order('createtime desc')->paginate($length);
    }

    public function images()
    {
        return $this->hasOne(CircleImages::class, 'circle_id', 'id');
    }

    public function design()
    {
        return $this->hasOne(Designs::class, 'id', 'design_id');
    }

    public function comments()
    {
        return $this->hasMany(CircleComments::class, 'circle_id', 'id')->where('online', 'yes');
    }

    public function like()
    {
        return $this->hasMany(CircleLikes::class, 'circle_id', 'id');
    }

    public static function getCircleById($circle_id)
    {
        $circle = self::with([
            'images',
            'design'
        ])->withCount(['like', 'comments'])->find($circle_id);
        return $circle;
    }


    public static function getCircleCount($design_id)
    {
        return self::where('design_id', $design_id)->count();
    }

    public static function getUserCircles($circle_id, $length = 15)
    {
        return self::with('design')->withCount('like')->whereIn('id', $circle_id)->order('createtime desc')->paginate($length);
    }

    public static function getCirclesByDesignId($design_id, $length = 15)
    {
        return self::with(['design'])->withCount('like')->where('design_id', $design_id)->order('createtime desc')->paginate($length);
    }
}
